package com.chx.three.mama.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.chx.three.mama.R;
import com.chx.three.mama.adapter.FeelingDiaryAdapter;
import com.chx.three.mama.model.FeelingDiary;
import com.chx.three.mama.task.FeelingDiaryTask;
import com.chx.three.mama.utils.BitmapHelper;
import com.chx.three.mama.utils.TaskProcessor;
import com.chx.three.mama.view.Topbar;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 心情日记界面
 */
@ContentView(R.layout.activity_feeling_diary)
public class FeelingDailyActivity extends Activity implements Topbar.onTopbarClickListener, PullToRefreshBase.OnRefreshListener2, TaskProcessor, View.OnClickListener {
    @ViewInject(R.id.add_diary)
    ImageView mAddDiary;
    @ViewInject(R.id.diary_listView)
    PullToRefreshListView mRefreshListView;
    @ViewInject(R.id.felling_diary_topbar)
    Topbar mTopbar;
    @ViewInject(R.id.anim_layout)
    private RelativeLayout mLayout;
    @ViewInject(R.id.loading_img)
    ImageView mLoadImg;

    private List<FeelingDiary> mList;

    private boolean mIsPressed;
    private FeelingDiaryAdapter mAdapter;

    private static final String TAG = "FeelingDailyActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        BitmapHelper.initBitmapHelper(this);

        mTopbar.setOnTopbarClickListener(this);
        mAddDiary.setOnClickListener(this);

        ListView listView = mRefreshListView.getRefreshableView();

        mList = new ArrayList<>();

        mAdapter = new FeelingDiaryAdapter(this, mList);
        listView.setAdapter(mAdapter);
        mAdapter.setDeleteListener(this);

        Drawable mBackGround = mLoadImg.getBackground();
        if (mBackGround instanceof AnimationDrawable) {
            AnimationDrawable mAnim = (AnimationDrawable) mBackGround;
            mAnim.start();
        }

        //设置加载时的动态图片
        Resources res = getResources();
        Drawable drawable = res.getDrawable(R.drawable.xlistview_arrow);
        mRefreshListView.setLoadingDrawable(drawable);

        //设置上拉和下拉都加载
        mRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        //设置加载中的标题
        mRefreshListView.setRefreshingLabel("数据更新中");
        mRefreshListView.setOnRefreshListener(this);

        FeelingDiaryTask task = new FeelingDiaryTask(this);
        task.execute();
    }

    @Override
    public void onLeftButtonClick() {
        finish();
    }

    @Override
    public void onRightButtonClick() {
        for (int i = 0; i < mList.size(); i++) {
            FeelingDiary diary = mList.get(i);
            if (mIsPressed) {
                mTopbar.setRightTextTitle("编辑");
                diary.setShow(false);
            } else {
                mTopbar.setRightTextTitle("完成");
                diary.setShow(true);

            }
        }
        mIsPressed = !mIsPressed;
        mAdapter.notifyDataSetChanged();

    }

    @Override
    public void onSearchButtonClick() {

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        FeelingDiaryTask task = new FeelingDiaryTask(this);
        task.execute();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        FeelingDiaryTask task = new FeelingDiaryTask(this);
        task.execute();
    }

    @Override
    public void processResult(JSONObject result) {
        mList.clear();
        if (result != null) {
            try {
                JSONArray array = result.getJSONArray("data");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    JSONArray data2 = object.getJSONArray("data");
                    for (int j = 0; j < data2.length(); j++) {
                        JSONObject object2 = data2.getJSONObject(j);
                        FeelingDiary diary = new FeelingDiary();
                        diary.parseJson(object2);
                        mList.add(diary);
                    }
                    mAdapter.notifyDataSetChanged();
                    mLayout.setVisibility(View.INVISIBLE);
                    mRefreshListView.onRefreshComplete();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.add_diary:
                Intent intent = new Intent(this, AddDiaryActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.delete_diary:
                Object tag = v.getTag();
                if (tag != null) {
                    if (tag instanceof Integer) {
                        final int pos = (Integer) tag;
                        FeelingDiary diary = mList.get(pos);
                        String mId = diary.getId();
                        String url = "http://baby.ci123.com/yunqi/ios2/diary_del.php?o_user_id=4268783&id=" + mId;
                        HttpUtils httpUtils = new HttpUtils();
                        httpUtils.send(HttpRequest.HttpMethod.GET,
                                url,
                                new RequestCallBack<String>() {
                                    @Override
                                    public void onSuccess(ResponseInfo<String> responseInfo) {
                                        String str = responseInfo.result;

                                        try {
                                            JSONObject object = new JSONObject(str);
                                            int ret = Integer.parseInt(object.getString("ret"));
                                            if (ret == 1) {
                                                mList.remove(pos);
                                                mAdapter.notifyDataSetChanged();
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }

                                    @Override
                                    public void onFailure(HttpException e, String s) {
                                        Toast.makeText(FeelingDailyActivity.this, "删除失败", Toast.LENGTH_LONG).show();
                                    }
                                }
                        );
                    }
                }
                break;
        }
    }
}
